
Imports System.Diagnostics
Imports log4net
Imports NHibernate
Imports NHibernate.Cfg
Imports System.Collections
Namespace NCS

    Class theProgram


        Friend Shared Sub Main(ByVal args As String())
            'childDel()
            'childAdd()
            'testCurveData()
            'newmethod()
            'newSample()
            'newChanel()
            'newusehistory()
            'newcurvehistory()
            'newweight()
            'newdichanel()
            'newdochanel()
            'testuser1()
            'Dim f As New Form1
            'f.ShowDialog()
            'testvoltnew()
            testcalcField()
        End Sub
        Private Shared Sub testcalcField()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim nvlv As Integer
            Dim o As Object
            o = session.CreateQuery("select max(dc.id) from doChanel dc").UniqueResult()
            If Not o Is Nothing Then
                nvlv = CInt(o) + 1
            Else
                nvlv = 0
            End If
            'Dim s As sample
            's = New sample
            's.c = 0.1
            'session.save(s)
            'session.flush()
        End Sub
        Private Shared Sub testuser1()

            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim o As IList
            o = session.CreateCriteria(GetType(muser)).List()

        End Sub
        Private Shared Sub testvoltnew()

            'Dim configuration As Configuration = Class1.configuration
            'Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            'Dim session As ISession = factory.OpenSession()
            ''Dim o As IList
            ''Dim v As VoltNew
            ''v = TryCast(session.Get(GetType(VoltNew), 18), VoltNew)
            'Dim mysig As Sig
            'mysig.Initialize()
            'loadsignew(session, mysig, 18)
            'savesignew(session, mysig, -1)
            'loadsignew(session, mysig, -1)
        End Sub

        'usehistory
        'CurveHis
        'diChanel
        Private Shared Sub newnetset()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New netSet
            newmethod1.ip = "127.0.0.1"
            newmethod1.id = 18
            session.Save(newmethod1)
            session.Flush()
        End Sub

        Private Shared Sub newdochanel()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New doChanel
            newmethod1.cname = "sample1"
            newmethod1.id = 18
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub newdichanel()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New diChanel
            newmethod1.cname = "sample1"
            newmethod1.id = 18
            session.Save(newmethod1)
            session.Flush()
        End Sub

        Private Shared Sub newweight()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New weight
            newmethod1.name = "sample1"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub newcurvehistory()
            'Dim configuration As Configuration = Class1.configuration
            'Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            'Dim session As ISession = factory.OpenSession()
            'Dim newmethod1 As New CurveHis
            'newmethod1.ele = "C"
            'session.Save(newmethod1)
            'session.Flush()
        End Sub

        Private Shared Sub newusehistory()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New usehistory
            newmethod1.endtime = Now()
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub newSample()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New SampleAve
            newmethod1.name = "a"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub newChanel()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New chanel
            newmethod1.ele = "a"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub newmethod()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim newmethod1 As New method
            newmethod1.cname = "all casade"
            newmethod1.cuser = "general"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub testCurveData()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim cdata As NCS.curveData
            'm = DirectCast(session.CreateSQLQuery("select * from curvedata").AddEntity(GetType(NCS.curveData)).List()(0), NCS.curveData)
            'm.jieqi = False
            'm.flushtime = 6
            'm.curveXishuList(0).x0 = 0.9
            cdata = New curveData
            Dim cxishu As curveXishu
            cxishu = New NCS.curveXishu
            cxishu.x0 = 0
            cxishu.x1 = 0
            cxishu.ele = "C"
            cxishu.cishu = 1
            Dim newmethod1 As New method
            newmethod1.cname = "all casade"
            newmethod1.cuser = "general"
            



            cxishu.method = newmethod1
            newmethod1.curveXishuList.Add(cxishu)
            'session.Save(cxishu)
            'session.Flush()
            cdata.curveXishu = cxishu
            cxishu.curveDataList.Add(cdata)
            'session.Save(cdata)
            session.Save(newmethod1)
            session.Flush()
        End Sub
        Private Shared Sub childAdd()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim m As NCS.method
            m = DirectCast(session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(GetType(NCS.method)).List()(0), NCS.method)
            'm.jieqi = false;
            'm.flushtime = 6;
            'm.curveXishuList[0].x0 = 0.9;
            Dim x1 As New NCS.curveXishu()
            x1.ele = "C"
            'x1.methodid = m.ID;
            x1.method = m
            m.curveXishuList.Add(x1)
            session.Save(x1)
            session.Flush()
        End Sub


        Private Shared Sub childDel()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim m As NCS.method
            m = DirectCast(session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(GetType(NCS.method)).List()(0), NCS.method)
            'm.jieqi = false;
            'm.flushtime = 6;
            'm.curveXishuList[0].x0 = 0.9;
            Dim x1 As NCS.curveXishu
            x1 = m.curveXishuList(0)
            m.curveXishuList.Remove(x1)
            'x1.method = null;
            session.Delete(x1)
            session.Flush()
        End Sub

        Private Shared Sub testMethod()
            Dim configuration As Configuration = Class1.configuration
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim m As NCS.method
            m = DirectCast(session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(GetType(NCS.method)).List()(0), NCS.method)
            m.jieqi = False
            m.flushtime = 6
            m.curveXishuList(0).x0 = 0.9
            session.Flush()
        End Sub
        Private Shared Sub testUser()

            ' ----------------------------------------
            ' Step 1: log4net configuration (optional)
            ' ----------------------------------------
            '
            ' The first step is to configure log4net, an open source logging
            ' framework that can be found at http://logging.apache.org/log4net/.
            ' NHibernate uses log4net to output warnings, debug messages, etc.
            ' It is strongly recommended that you enable log4net when
            ' troubleshooting your application with NHibernate.
            '
            ' The statement below instructs log4net to configure itself
            ' by loading its settings from the application configuration file.
            ' See app.config for more details; however, it is beyond the scope
            ' of this sample to discuss log4net in detail.  Refer to the
            ' above web site to learn more.
            '
            ' This particular sample is configured to write to a log file
            ' called log-file.txt.  It will be created in the same folder
            ' as the executable.

            log4net.Config.XmlConfigurator.Configure()


            ' ---------------------
            ' Step 2: Configuration
            ' ---------------------
            '
            ' A Configuration object manages global settings as well as
            ' the mappings between your classes and the database.  You
            ' will always create a Configuration object.

            Dim configuration As New Configuration()

            ' The Configure method (without parameters) instructs NHibernate
            ' to look for its configuration from app.config.  If no
            ' configuration can be found, it then looks for an XML file
            ' with the name hibernate.cfg.xml.  This sample uses the XML
            ' file instead of app.config.
            '
            ' Note: hibernate.cfg.xml has been added as a content file to
            ' this project.  The file is configured to be copied to the
            ' output folder (check the Copy to Output Directory property
            ' of the file from the Solution Explorer panel).

            Console.WriteLine("Loading hibernate.cfg.xml...")
            configuration.Configure()

            ' The next step is to load and parse all of the files that
            ' define the mappings between your objects and the underlying
            ' database.  There are many ways to do this (refer to the
            ' NHibernate documentation).  The technique below instructs
            ' NHibernate to search for all mapping files that have
            ' been embedded in the assembly.

            Console.WriteLine("Loading mapping files in this executable...")
            configuration.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly())


            ' --------------------------------
            ' Step 3: Create a Session Factory
            ' --------------------------------
            '
            ' An ISessionFactory is used to create NHibernate sessions.  A 
            ' session represents a connection to the database with all of
            ' the functionality provided by NHibernate.  

            Dim factory As ISessionFactory = configuration.BuildSessionFactory()


            ' ---------------------
            ' Test 1: Create a muser
            ' ---------------------

            Dim firstId As String

            Using session As ISession = factory.OpenSession()

                Console.WriteLine("Saving a test muser object to the database...")

                Dim user As New muser()
                user.mname = "hello3"
                'user.EmailAddress = TestEmailAddress;
                'user.Password = TestPassword;
                'user.UserName = TestUserName;

                ' Save the user object, then flush the session to
                ' commit it to the database.  Upon return, the
                ' user object should have a new ID.

                session.Save(user)
                session.Flush()

                firstId = user.mname

                Console.WriteLine("id" & user.ID.ToString())

                'Debug.Assert(
                '    firstId > 0, "The Id should have been returned.");

                ' If you get a failure here (e.g. because user.Id is zero),
                ' make sure the hibernate.connection.release_mode setting
                ' in hibernate.cfg.xml is set to "on_close".
                '
                ' Explanation:  The Id of the underlying Users table is an
                ' AutoNumber. This means Access/Jet assigns the value of
                ' the ID when the record is first created.  The Jet driver
                ' for NHibernate retrieves the new ID by sending a special
                ' SQL statement ("select @@identity").  The @@Identity 
                ' variable contains the last ID generated by Jet.  
                '
                ' However, NHibernate is aggressive in closing the
                ' ADO.NET database connection between SQL statements.  This
                ' is the correct design (normally) but has the unfortunate
                ' side-effect of invalidating the @@Identity variable.
                ' NHibernate closes the connection between the INSERT
                ' statment (when saving) and the SELECT @@Identity statement.
                '
                ' The temporary work around is to enable the older behavior
                ' of forcing NHibernate to keep the underlying connection open
                ' throughout the lifetime of the session.  This will likely
                ' be fixed in a future version of the library.
                Console.WriteLine("The muser object was assigned an ID of " & firstId & "!")
            End Using


            ' ---------------------
            ' Test 2: Load the User
            ' ---------------------
            '
            ' Try reloading the user back from the database.  Compare
            ' the fields with the properties originally saved.  This is
            ' performed with a completely different session and User object
            ' to show that it was truly loaded from the database.

            Using session As ISession = factory.OpenSession()

                Console.WriteLine("Attempting to reload User with ID " & firstId & "...")

                Dim user As muser = session.Load(Of muser)(firstId)
                Console.WriteLine("id" & user.ID.ToString())
                Debug.Assert(user.mname = firstId, "The wrong Id field was returned.")
                user = session.Load(Of muser)(firstId)
                Console.WriteLine("id" & user.ID.ToString())
                'Debug.Assert(
                '    user.EmailAddress == TestEmailAddress,
                '    "The wrong EmailAddress was returned.");

                'Debug.Assert(
                '    user.Password == TestPassword,
                '    "The wrong Password was returned.");

                'Debug.Assert(
                '    user.UserName == TestUserName,
                '    "The wrong UserName was returned.");


                Console.WriteLine("The user was reloaded successfully!")
            End Using


            ' -----------------------
            ' Test 3: Delete the muser
            ' -----------------------

            Using session As ISession = factory.OpenSession()

                ' Load the user again (to get an object reference),
                ' then delete it from the database.

                Console.WriteLine("Deleting the user...")
                Dim user As muser = session.Load(Of muser)(firstId)
                session.Delete(user)

                ' Now try to reload the user.  This should fail.

                Try
                    Console.WriteLine("Confirming the user record is deleted...")
                    Dim deletedUser As muser = session.[Get](Of muser)(firstId)
                    Throw New InvalidOperationException("The load should not have succeeded.")
                Catch ex As ObjectDeletedException
                    ' eat
                    Console.WriteLine(ex.Message)
                End Try

                session.Flush()
            End Using


            ' I hope this sample was useful.  Free free to contact
            ' me at dave@fairmetric.com if you have questions or feedback.

            Console.WriteLine("Done.  Press Enter to exit...")
            Console.ReadLine()

        End Sub
    End Class
End Namespace
